package yy;

import java.util.Stack;

public class IsValid {
    public static void main(String[] args){
        String s = "(";
        Boolean result = isValid(s);
        System.out.println(result);
    }

    public static boolean isValid(String s) {
        char[] str = s.toCharArray();
        Stack<Character> stack = new Stack<>();
        for(int i = 0; i<str.length; i++){
            if(str[i]=='(' || str[i]=='{' || str[i]=='['){
                stack.push(str[i]);
                continue;
            }
            if(str[i]==')' || str[i]=='}' || str[i]==']'){
                if(!stack.isEmpty()){
                    char temp = stack.peek();
                    if((temp=='(' && str[i]==')') || (temp=='{' && str[i]=='}') || (temp=='[' && str[i]==']')){
                        stack.pop();
                    }else {
                        return false;
                    }
                }else {
                    return false;
                }
            }
        }
        if(!stack.isEmpty()){
            return false;
        }
        return true;

    }

}
